package com.xoa.dev.efficiency.service.impl;

import com.xoa.dao.users.UsersMapper;
import com.xoa.dev.efficiency.dao.FlowParaMapper;
import com.xoa.dev.efficiency.model.CodeTransform;
import com.xoa.dev.efficiency.model.FlowPara;
import com.xoa.dev.efficiency.model.ModifyPwd;
import com.xoa.model.users.Users;
import com.xoa.dev.efficiency.service.FlowParaService;
import com.xoa.util.MD5Utils;
import com.xoa.util.ToJson;
import com.xoa.util.encrypt.EncryptSalt;
import net.sf.json.JSONArray;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FlowParaServiceImpl implements FlowParaService {
    @Resource
    FlowParaMapper flowParaMapper;
    @Autowired
    UsersMapper usersMapper;

    public ToJson<FlowPara> insertNotes(FlowPara flowPara) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            flowParaMapper.insertFlowPara(flowPara);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson selectFlowPara(FlowPara flowPara) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        ToJson<CodeTransform> json1 = new ToJson<CodeTransform>();
        List<CodeTransform> codeTansformList = new ArrayList<CodeTransform>();
        try {
            String paraName = flowPara.getParaName();
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", paraName);
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            if (flowPara.getParaName() != null) {
                if (flowPara.getParaName().trim().equals("FLOW_JUBU_ID")) {
                    String paraValue = flowParaList.get(0).getParaValue();
                    if (paraValue != null && !paraValue.isEmpty()) {
                        String tmpStr = paraValue.substring(0, paraValue.length() - 2);
                        String[] strCode = tmpStr.split(",");
                        //List<CodeTransform> codeTansformList= new ArrayList<CodeTransform>();
                        for (int i = 0; i < strCode.length; i++) {
                            if (strCode[i] != null) {
                                Users users = usersMapper.getUserId(strCode[i]);
                                CodeTransform codeTransform = new CodeTransform();
                                codeTransform.setParaname("FLOW_JUBU_ID");
                                codeTransform.setCode(strCode[i]);
                                codeTransform.setName(users.getUserName());

                                codeTansformList.add(i, codeTransform);
                            }
                        }
                    }
                }
                if (flowPara.getParaName().trim().equals("FLOW_LIULAN_ID")) {
                    String paraValue = flowParaList.get(0).getParaValue();
                    if (paraValue != null && !paraValue.isEmpty()) {
                        String tmpStr = paraValue.substring(0, paraValue.length() - 2);
                        String[] strCode = tmpStr.split(",");
                        //List<CodeTransform> codeTansformList= new ArrayList<CodeTransform>();
                        for (int i = 0; i < strCode.length; i++) {
                            if (strCode[i] != null) {
                                Users users = usersMapper.getUserId(strCode[i]);
                                CodeTransform codeTransform = new CodeTransform();
                                codeTransform.setParaname("FLOW_LIULAN_ID");
                                codeTransform.setCode(strCode[i]);
                                codeTransform.setName(users.getUserName());

                                codeTansformList.add(i, codeTransform);
                            }
                        }
                    }
                }
                if (flowPara.getParaName().trim().equals("FLOW_QUANMIAN_ID")) {
                    String paraValue = flowParaList.get(0).getParaValue();
                    if (paraValue != null && !paraValue.isEmpty()) {
                        String tmpStr = paraValue.substring(0, paraValue.length() - 2);
                        String[] strCode = tmpStr.split(",");
                        //List<CodeTransform> codeTansformList= new ArrayList<CodeTransform>();
                        for (int i = 0; i < strCode.length; i++) {
                            if (strCode[i] != null) {
                                Users users = usersMapper.getUserId(strCode[i]);
                                CodeTransform codeTransform = new CodeTransform();
                                codeTransform.setParaname("FLOW_QUANMIAN_ID");
                                codeTransform.setCode(strCode[i]);
                                codeTransform.setName(users.getUserName());

                                codeTansformList.add(i, codeTransform);
                            }

                        }
                    }
                }
                if (flowPara.getParaName().trim().equals("FLOW_JUBU_ID") || flowPara.getParaName().trim().equals("FLOW_LIULAN_ID") || flowPara.getParaName().trim().equals("FLOW_QUANMIAN_ID")) {
                    json1.setObj(codeTansformList);
                    json1.setFlag(0);
                    json1.setMsg("ok");
                    return json1;
                } else {
                    json.setObj(flowParaList);
                    json.setFlag(0);
                    json.setMsg("ok");
                    return json;
                }
            } else {
                json.setObj(flowParaList);
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }


        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            return json;
        }
    }

    public ToJson<FlowPara> updateByPrimaryKey(FlowPara flowPara) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            String paraName = flowPara.getParaName();
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", paraName);
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            if (paraName.trim().equals(flowParaList.get(0).getParaName())) {
                flowParaMapper.updateByPrimaryKey(flowPara);
                json.setFlag(0);
                json.setMsg("ok");
            } else {
                flowParaMapper.insertFlowPara(flowPara);
                json.setFlag(0);
                json.setMsg("ok");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowPara> modifyPwd(ModifyPwd modify) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", "SUPER_PASS");
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            if (modify.getOldpassword().equals(flowParaList.get(0).getParaValue())) {
                FlowPara flowPara = new FlowPara();
                flowPara.setParaName(flowParaList.get(0).getParaName());
                flowPara.setParaValue(modify.getNewpassword());
                flowParaMapper.updateByPrimaryKey(flowPara);

                json.setFlag(0);
                json.setMsg("ok");
            } else {
                json.setFlag(1);
                json.setMsg("原密码验证错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowPara> modifyPwd_1(ModifyPwd modify) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", "SUPER_PASS");
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            String dbPassword = flowParaList.get(0).getParaValue();
            String oldpassword = modify.getOldpassword();
            if (isPasswordCorrect(oldpassword, dbPassword)) {
                FlowPara flowPara = new FlowPara();
                flowPara.setParaName(flowParaList.get(0).getParaName());
                String newPassword = modify.getNewpassword();
                String encodedNewPassword = md5Encode(newPassword);
                flowPara.setParaValue(encodedNewPassword);
                flowParaMapper.updateByPrimaryKey(flowPara);

                json.setFlag(0);
                json.setMsg("ok");
            } else {
                json.setFlag(1);
                json.setMsg("原密码验证错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowPara> logincheck(String pwd, HttpServletRequest request) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", "SUPER_PASS");
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            if (pwd.equals(flowParaList.get(0).getParaValue())) {
                request.getSession().setAttribute("SUPER_PASS_ABILITY_FLAG", true);
                json.setFlag(0);
                json.setMsg("ok");
            } else {
                json.setFlag(1);
                json.setMsg("密码验证错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowPara> logincheck_1(String pwd, HttpServletRequest request) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("paraName", "SUPER_PASS");
            List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
            String dbPassword=flowParaList.get(0).getParaValue();
            if (isPasswordCorrect(pwd,dbPassword)) {
                request.getSession().setAttribute("SUPER_PASS_ABILITY_FLAG", true);
                json.setFlag(0);
                json.setMsg("ok");
            } else {
                json.setFlag(1);
                json.setMsg("密码验证错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    @Override
    public ToJson modify(String jsonStr) {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        List<FlowPara> list = (List<FlowPara>) JSONArray.toList(JSONArray.fromObject(jsonStr), FlowPara.class);
        for (FlowPara flowPara : list) {
            try {
                String paraName = flowPara.getParaName();
                Map<String, Object> map = new HashMap<>();
                map.put("paraName", paraName);
                List<FlowPara> flowParaList = flowParaMapper.selectFlowPara(map);
                if (paraName.trim().equals(flowParaList.get(0).getParaName())) {
                    flowParaMapper.updateByPrimaryKey(flowPara);
                    json.setFlag(0);
                    json.setMsg("ok");
                } else {
                    flowParaMapper.insertFlowPara(flowPara);
                    json.setFlag(0);
                    json.setMsg("ok");
                }
            } catch (Exception e) {
                e.printStackTrace();
                json.setFlag(1);
                json.setMsg("err");
            }
        }


        return json;
    }


    public ToJson selectFlowParaByQuan() {
        ToJson<FlowPara> json = new ToJson<FlowPara>();
        try {
            List<FlowPara> flowParaList = flowParaMapper.selectFlowParaByQuan();
            for (FlowPara para : flowParaList) {
                String paraValue = para.getParaValue();
                if (paraValue != null && !paraValue.isEmpty()) {
                    /*String tmpStr = paraValue.substring(0, paraValue.length() - 2);*/
                    String[] strCode = paraValue.split(",");
                    String userName = "";
                    for (int i = 0; i < strCode.length; i++) {
                        if (strCode[i] != null) {
                            Users users = usersMapper.getUserId(strCode[i]);
                            if (users != null) {
                                userName += users.getUserName() + ',';
                                para.setParaNameStr(userName);
                            }
                        }
                    }
                }

            }

            if (flowParaList.size() > 0) {
                json.setObj(flowParaList);
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            } else {
                json.setFlag(1);
                json.setMsg("err");
                return json;
            }


        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            return json;
        }
    }

    @Override
    public ToJson settingFlowId(String flowIdF, String flowIdS) {
        ToJson toJson = new ToJson();
        int line;
        try {
            if (flowIdF == null || flowIdS == null) {
                flowIdF = "";
                flowIdS = "";
            }
            //父流程
            FlowPara flowPara = flowParaMapper.selectInfo(flowIdF);
            if (flowPara != null) {
                line = flowParaMapper.FlowIdF(flowIdF);
            } else {
                line = flowParaMapper.addFlowPara("FLOW_ID_F", flowIdF);
            }
            //子流程
            FlowPara flowPara1 = flowParaMapper.selectInfo(flowIdS);
            if (flowPara1 != null) {
                line = flowParaMapper.FlowIdS(flowIdS);
            } else {
                line = flowParaMapper.addFlowPara("FLOW_ID_S", flowIdF);
            }
            if (line > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    private boolean isPasswordCorrect(String inputPassword, String dbPassword) {
        return dbPassword.equals(md5Encode(inputPassword, dbPassword));
    }

    private String md5Encode(String password, String salt) {
        return MD5Utils.md5Crypt(password.trim().getBytes(), salt);
    }

    private String md5Encode(String password) {
        String salt = "$1$".concat(EncryptSalt.getRandomSalt(12));
        return md5Encode(password, salt);
    }

}
